<template>
  <el-dialog
    title="修改用户角色权限"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="用户id" prop="userName">
        <el-input v-model="dataForm.id" placeholder="用户id" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="手机号/账号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号/账号" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="课本角色" size="mini" prop="roleIdList">
        <el-checkbox-group v-model="dataForm.roleIdList">
          <el-checkbox
            v-for="role in roleList"
            :key="role.id"
            :label="role.id"
            >{{ role.roleName }}</el-checkbox
          >
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      roleList: [],
      userId: 1,
      cityList: [],
      dataForm: {
        id: 0,
        mobile: "",
        roleIdList: [],
      },
    };
  },
  created() {
    this.$http({
      url: this.$http.adornUrl(`/sf/city/getCityList`),
      method: "get",
    }).then(({ data }) => {
      this.cityList = data.cityList;
    });
  },
  methods: {
    init(id) {
      this.dataForm.id = id;
      this.$http({
        url: this.$http.adornUrl("/sf/bookrole/select"),
        method: "get",
        params: this.$http.adornParams(),
      })
        .then(({ data }) => {
          this.roleList = data && data.code === 0 ? data.list : [];
        })
        .then(() => {
          this.visible = true;
        })
        .then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sf/teacher/info/${this.dataForm.id}`),
              method: "get",
              params: this.$http.adornParams(),
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.dataForm.id = data.teacher.id;
                this.dataForm.mobile = data.teacher.mobile;
                this.dataForm.roleIdList = data.teacher.bookRoleIds;
              }
            });
          }
        });
    },
    // 表单提交
    dataFormSubmit() {
      this.$http({
        url: this.$http.adornUrl(`/sf/teacher/saveTeacherRole`),
        method: "post",
        data: this.$http.adornParams({
          id: this.dataForm.id,
          bookRoleIds: this.dataForm.roleIdList,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.visible = false;
            },
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
  },
};
</script>
